﻿using CommonMvc.Common;
using CommonMvc.Models.CommonModels;
using CommonMvc.Models.Concreate;
using CommonMvc.Models.Sys;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace CommonMvc.Filters
{

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class BaseAuthorizationFilter : Attribute, IActionFilter
    {
        bool _isCheck = true;
        List<String> _checkPermissions = new List<string>();
        protected DataContext _dataBaseContext;
        protected ICookieManager _cookieManager;
        protected IWorkContext _workContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cookieManager"></param>
        /// <param name="workContext"></param>
        /// <param name="dataContext"></param>
        /// <param name="isCheck"></param>
        /// <param name="checkPermissions"> 以,分割不同权限  以 | 分割权限中的控制器和方法</param>
        public BaseAuthorizationFilter(ICookieManager cookieManager,IWorkContext workContext,DataContext dataContext,bool isCheck = true, string checkPermissions = "")
        {
            _isCheck = isCheck;
            if (checkPermissions != null && checkPermissions.Length > 0)
            {
                //_checkPermissions = checkPermissions.ToList();
            }          
            _workContext = workContext; 
            _cookieManager = cookieManager; 
            _dataBaseContext = dataContext; 

        }
        public bool IsCheck
        {
            get { return _isCheck; }
            set { _isCheck = value; }
        }
        public List<string> CheckPermissions
        {
            get { return _checkPermissions; }
            set { _checkPermissions = value; }
        }

        public bool CheckAuthorize(AuthorizationFilterContext context) {

            return true;
        }

        
        public void OnAuthorization(AuthorizationFilterContext context)
        {
           
            
            //throw new NotImplementedException();
        }

        public JsonResult GeneJsonFailResult(String message = "没有访问权根") {
            var data = new
            {
                Code = 1,
                Message = message
            };
            JsonResult result = new JsonResult(data);
            return result;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            
            if (!IsCheck)
            {
                return;
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            User user = _workContext.CurrentUser as User;
            PersonRole personRole = _dataBaseContext.PersonRoles.Where(p => p.PersonId == user.UserId).FirstOrDefault();
            if (personRole == null)
            {
                context.Result = GeneJsonFailResult();
            }
            List<RolePermission> permissionList = _dataBaseContext.RolePermissions.Where(p => p.RoleId == personRole.RoleId).ToList();
            List<Menu> allMenu = _dataBaseContext.Menus.ToList();
            List<Menu> parentMenu = new List<Menu>();
            foreach (Menu item in allMenu)
            {
                if (item.ParentId == 0)
                {
                    parentMenu.Add(item);
                }
            }
            foreach (Menu item in parentMenu)
            {
                foreach (Menu childitem in allMenu)
                {
                    if (childitem.ParentId == item.Id)
                    {
                        // if (context.Co)
                        item.ChilMenus.Add(childitem);
                    }
                }
            }
            var controller = context.Controller as Controller;
            if (controller == null) return;
            controller.ViewBag.MenuList = parentMenu;

            //应区分是   调用接口还是  访问网页
           // if (!CheckAuthorize(context))
            //{
               // context.Result = GeneJsonFailResult();
          //  }
            //throw new NotImplementedException();
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            //throw new NotImplementedException();
        }
    }
}
